<?php
class ApiCenter_ProductApi_Model_Api2_Product_Rest_Admin_V1 extends  ApiCenter_ProductApi_Model_Api2_Product_Rest{
    
    
    protected function _retrieveCollection(){
        $vendorId  =$this->getRequest()->getParam('vendorid');
        
        try {
 
            $collection =$this->_getCollectionForRetrieveCollection($vendorId);
            foreach ($collection as $key => $product){
                $list[$key] = $product->getData();
            }
            $list['total']=$this->_getCountForRetrieveCollection($vendorId);
            $res['status'] =1;
            $res['info'] =$list;
        } catch (Exception $e) {
            $res['status'] =0;
            $res['info'] =$e->getMessage();
        }

        return $res;
    }
  protected function _retrieve(){
        $vendorId  =$this->getRequest()->getParam('vendorid');
        $productId = $this->getRequest()->getParam('id');
      
		try {
	    $collection = Mage::getModel("catalog/product")->getCollection();
	    $collection->addFieldToFilter("vendor_id",$vendorId);
	    $collection->addFieldToFilter("entity_id",$productId);
        if(empty($collection->getData())){
            $this->_critical("商品不存在!",400);  
        }
        $product = Mage::getModel("catalog/product")->load($productId);
        //属性组id
        $set_id = $product->getAttributeSetId();
        
        $setArr = $this->_loadAttrMatchValue($set_id,$product);
        $res["status"] =1;
        $res["info"] =$setArr;
	      } catch (Exception $e) {
            $res["status"] =0;
            $res["info"] =$e->getMessage();
        }
        return $res;
    }
    
    protected  function _delete(){
        Mage::log("is_delete");
        $productId = $this->getRequest()->getParam('id');
        $childs ="";
        $approval = "";
       try {
      
          $product =  Mage::getModel("catalog/product")->load($productId);
          $approval = $product->getApproval();
          $type = $product->getTypeId();
          //3:产品被拒绝  0：未提交
          if($type == "configurable"){
              $childs = $this->_loadchildproduct($product,true);
          }
          if($approval==="3" || $approval==="0"){
              try {
                   if(!empty($childs)){
                       foreach ($childs as $p){
                           $record[]=$p->getId();
                       }
                       foreach ($childs as $_products){
                           $_products->delete();
                       }
                   }

              } catch (Exception $e) {
                  Mage::log("子产品删除失败!");
                  Mage::log("待删除的子产品：");
                  Mage::log($record);
              }
              Mage::log($product->getSku(),1,"deleteproduct.log");
                $product->delete();
          }
          $res['status'] =1;
          $res["info"] = "产品删除成功";
       } catch (Exception $e) {
           $res["status"] =0;
           $res["info"] ="产品删除失败";
       }   
       $this->_render($res);
    }
    
}